decisions = {

	add_castle_buildings = {
		potential = {
			#ai = yes
			is_feudal = yes
		}
		allow = {
			any_demesne_title = { 
				tier = baron
				location = {
					has_province_modifier = building_setup
				}
				OR = {
					NOT = { has_building = ca_tiltyard_1 }
					NOT = { has_building = ca_range_1 }
					NOT = { has_building = ca_tiltyard_1a }
					NOT = { has_building = ca_range_1a }
				}
			}
		}
		effect = {		
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = feudal_HC
					}
					NOT = { has_building = ca_tiltyard_1 }
				}
				if = { 
					limit = { has_building = ca_barracks_2 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_pike_1a
				}
				if = { 
					limit = { has_building = ca_barracks_4 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_pike_2a
				}
				if = { 
					limit = { has_building = ca_barracks_6 location = { TECH_CASTLE_CONSTRUCTION = 5 } }
					add_building = ca_pike_3a
				}
				if = { 
					limit = { has_building = ca_stable_1 }
					add_building = ca_tiltyard_1
				}
				if = { 
					limit = { has_building = ca_stable_2 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_tiltyard_2
				}
				if = { 
					limit = { has_building = ca_stable_4 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_tiltyard_3
				}
				if = { 
					limit = { has_building = ca_stable_6 location = { TECH_CASTLE_CONSTRUCTION = 5 } }
					add_building = ca_tiltyard_4
				}
				if = { 
					limit = { has_building = ca_stable_1 }
					add_building = ca_men_at_arms_1
				}
				if = { 
					limit = { has_building = ca_stable_2 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_men_at_arms_2
				}
				if = { 
					limit = { has_building = ca_stable_4 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_men_at_arms_3
				}
				if = { 
					limit = { has_building = ca_stable_6 location = { TECH_CASTLE_CONSTRUCTION = 5 } }
					add_building = ca_men_at_arms_4
				}
			}
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = feudal_LC
					}
					NOT = { has_building = ca_tiltyard_1 }
				}
				if = { 
					limit = { has_building = ca_militia_barracks_1 }
					add_building = ca_range_1a
				}
				if = { 
					limit = { has_building = ca_militia_barracks_2 location = { TECH_CASTLE_CONSTRUCTION = 2 } }
					add_building = ca_range_2a
				}
				if = { 
					limit = { has_building = ca_militia_barracks_3 location = { TECH_CASTLE_CONSTRUCTION = 4 } }
					add_building = ca_range_3a
				}
				if = { 
					limit = { has_building = ca_barracks_2 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_pike_1a
				}
				if = { 
					limit = { has_building = ca_barracks_4 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_pike_2a
				}
				if = { 
					limit = { has_building = ca_barracks_6 location = { TECH_CASTLE_CONSTRUCTION = 5 } }
					add_building = ca_pike_3a
				}
				if = { 
					limit = { has_building = ca_stable_1 }
					add_building = ca_tiltyard_1
				}
				if = { 
					limit = { has_building = ca_stable_2 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_tiltyard_2
				}
				if = { 
					limit = { has_building = ca_stable_4 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_tiltyard_3
				}
				if = { 
					limit = { has_building = ca_stable_6 location = { TECH_CASTLE_CONSTRUCTION = 5 } }
					add_building = ca_tiltyard_4
				}
			}
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = feudal_INF
					}
					NOT = { has_building = ca_range_1 }
				}
				if = { 
					limit = { has_building = ca_militia_barracks_1 }
					add_building = ca_range_1
				}
				if = { 
					limit = { has_building = ca_militia_barracks_2 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_range_2
				}
				if = { 
					limit = { has_building = ca_militia_barracks_3 location = { TECH_CASTLE_CONSTRUCTION = 2 } }
					add_building = ca_range_3
				}
				if = { 
					limit = { has_building = ca_militia_barracks_4 location = { TECH_CASTLE_CONSTRUCTION = 4 } }
					add_building = ca_range_4
				}
				if = { 
					limit = { has_building = ca_barracks_1 }
					add_building = ca_pike_1
				}
				if = { 
					limit = { has_building = ca_barracks_2 location = { TECH_CASTLE_CONSTRUCTION = 2 } }
					add_building = ca_pike_2
				}
				if = { 
					limit = { has_building = ca_barracks_4 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_pike_3
				}
				if = { 
					limit = { has_building = ca_barracks_6 location = { TECH_CASTLE_CONSTRUCTION = 5 } }
					add_building = ca_pike_4
				}
				if = { 
					limit = { has_building = ca_stable_1 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_tiltyard_1a
				}
				if = { 
					limit = { has_building = ca_stable_3 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_tiltyard_2a
				}
			}
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = semifeudal_CAV
					}
					NOT = { has_building = ca_tiltyard_1a }
				}
				if = { 
					limit = { has_building = ca_stable_1 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_tiltyard_1a
				}
				if = { 
					limit = { has_building = ca_stable_3 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_tiltyard_2a
				}
			}
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = semifeudal_INF
					}
					NOT = { has_building = ca_range_1a }
				}
				if = { 
					limit = { has_building = ca_militia_barracks_1 }
					add_building = ca_range_1a
				}
				if = { 
					limit = { has_building = ca_militia_barracks_2 location = { TECH_CASTLE_CONSTRUCTION = 2 } }
					add_building = ca_range_2a
				}
				if = { 
					limit = { has_building = ca_militia_barracks_3 location = { TECH_CASTLE_CONSTRUCTION = 4 } }
					add_building = ca_range_3a
				}
				if = { 
					limit = { has_building = ca_barracks_2 location = { TECH_CASTLE_CONSTRUCTION = 1 } }
					add_building = ca_pike_1a
				}
				if = { 
					limit = { has_building = ca_barracks_4 location = { TECH_CASTLE_CONSTRUCTION = 3 } }
					add_building = ca_pike_2a
				}
				if = { 
					limit = { has_building = ca_barracks_6 location = { TECH_CASTLE_CONSTRUCTION = 5 } }
					add_building = ca_pike_3a
				}
			}
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 1
		}
	}
	
	add_city_buildings = {
		potential = {
			#ai = yes
			is_republic = yes
		}
		allow = {
			any_demesne_title = { 
				tier = baron
				location = {
					has_province_modifier = building_setup
				}
				OR = {
					NOT = { has_building = ct_guard_1 }
					NOT = { has_building = ct_bows_1 }
				}
			}
		}
		effect = {		
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = city_pike
					}
					NOT = { has_building = ct_guard_1 }
				}
				if = { 
					limit = { has_building = ct_barracks_1  }
					add_building = ct_guard_1
				}
				if = { 
					limit = { has_building = ct_barracks_2 }
					add_building = ct_guard_2
				}
				if = { 
					limit = { has_building = ct_barracks_3 }
					add_building = ct_guard_3
				}
				if = { 
					limit = { has_building = ct_barracks_4 location = { TECH_CITY_CONSTRUCTION = 2 } }
					add_building = ct_guard_4
				}
				if = { 
					limit = { has_building = ct_barracks_5 location = { TECH_CITY_CONSTRUCTION = 3 } }
					add_building = ct_guard_5
				}
				if = { 
					limit = { has_building = ct_barracks_6 location = { TECH_CITY_CONSTRUCTION = 4 } }
					add_building = ct_guard_6
				}
			}
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = city_archery
					}
					NOT = { has_building = ct_bows_1 }
				}
				if = { 
					limit = { has_building = ct_barracks_1  }
					add_building = ct_bows_1
				}
				if = { 
					limit = { has_building = ct_barracks_2 }
					add_building = ct_bows_2
				}
				if = { 
					limit = { has_building = ct_barracks_3 }
					add_building = ct_bows_3
				}
				if = { 
					limit = { has_building = ct_barracks_4 location = { TECH_CITY_CONSTRUCTION = 2 } }
					add_building = ct_bows_4
				}
				if = { 
					limit = { has_building = ct_barracks_5 location = { TECH_CITY_CONSTRUCTION = 3 } }
					add_building = ct_bows_5
				}
				if = { 
					limit = { has_building = ct_barracks_6 location = { TECH_CITY_CONSTRUCTION = 4 } }
					add_building = ct_bows_6
				}
			}
			random_demesne_title = { 
				limit = {
					tier = baron
					location = {
						has_province_modifier = building_setup
						has_province_modifier = nonfeudal_province
					}
					NOT = { has_building = ct_cav_1 }
				}
				if = { 
					limit = { has_building = ct_barracks_1  }
					add_building = ct_cav_1
				}
				if = { 
					limit = { has_building = ct_barracks_3 location = { TECH_CITY_CONSTRUCTION = 2 } }
					add_building = ct_cav_2
				}
				if = { 
					limit = { has_building = ct_barracks_4 location = { TECH_CITY_CONSTRUCTION = 3 } }
					add_building = ct_cav_3
				}
				if = { 
					limit = { has_building = ct_barracks_5 location = { TECH_CITY_CONSTRUCTION = 4 } }
					add_building = ct_cav_4
				}
			}
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 1
		}
	}
		
	set_up_modifiers = {
		potential = {
			#ai = yes
		}
		allow = {
			any_demesne_province = { 
				NOT = { 
					OR = {
						has_province_modifier = feudal_HC
						has_province_modifier = feudal_LC
						has_province_modifier = feudal_INF
						has_province_modifier = semifeudal_CAV
						has_province_modifier = semifeudal_INF
						has_province_modifier = nonfeudal_province						
					}
				}
			} 
		}
		effect = {
			random_demesne_province = {
			
				#Castle troop modifiers
				limit = {
					NOT = { 
						OR = {
							has_province_modifier = feudal_HC
							has_province_modifier = feudal_LC
							has_province_modifier = feudal_INF
							has_province_modifier = semifeudal_CAV
							has_province_modifier = semifeudal_INF
							has_province_modifier = nonfeudal_province						
						}
					}
				}
				if = {
					limit = {
						owner = { 
							is_republic = yes
						}
					}
					add_province_modifier = { 
						name = nonfeudal_province
						duration = -1
					}
				}
				if = {
					limit = {
						owner = { is_theocracy = yes }
					}
					random_list = {
						50 = {
							add_province_modifier = { 
								name = semifeudal_CAV
								duration = -1
							}
						}
						50 = {
							add_province_modifier = { 
								name = semifeudal_INF
								duration = -1
							}
						}
					}
				}
				if = {
					limit = {
						owner = { is_feudal = yes }
					}
					random_list = {
						50 = {
							add_province_modifier = { 
								name = feudal_HC
								duration = -1
							}
						}
						50 = {
							add_province_modifier = { 
								name = feudal_LC
								duration = -1
							}
						}
						50 = {
							add_province_modifier = { 
								name = feudal_INF
								duration = -1
							}
						}
					}
					if = {
						limit = {	
							NOT = { num_of_settlements = 3 }	#Poor Provinces. Goes First
							has_province_modifier = feudal_HC
						}
						remove_province_modifier = feudal_HC
						add_province_modifier = { 
							name = feudal_INF
							duration = -1
						}
					}
					if = {
						limit = { 
							OR = {
								owner = { culture = frankish }	#Owner Culture. Goes Second
								owner = { culture = occitan }
								owner = { culture = norman }								
								owner = { culture = polish }								
								owner = { culture = hungarian }
							}
							has_province_modifier = feudal_INF
						}
						random = {
							chance = 50
							remove_province_modifier = feudal_HC
							add_province_modifier = { 
								name = feudal_INF
								duration = -1
							}
						}
					}
					if = {
						limit = { 								#Province Culture. Goes Third
							OR = {
								culture_group = celtic
								culture_group = west_germanic
								culture_group = north_germanic
								culture_group = finno_ugric
								culture_group = baltic
								culture_group = east_slavic
								culture_group = south_slavic
								culture = pommeranian
							}
							has_province_modifier = feudal_HC
						}
						random = {
							chance = 50
							remove_province_modifier = feudal_HC
							add_province_modifier = { 
								name = feudal_INF
								duration = -1
							}
						}
					}
					if = {
						limit = { 								#Province Culture. Goes Third
							OR = {
								culture_group = arabic
								culture_group = altaic
								culture = alan
							}
							NOT = { has_province_modifier = feudal_LC }
						}
						random = {
							chance = 50
							remove_province_modifier = feudal_HC
							remove_province_modifier = feudal_INF
							add_province_modifier = { 
								name = feudal_LC
								duration = -1
							}
						}
					}
				}
				
				#City troop modifiers
				random_list = {
					50 = {
						add_province_modifier = { 
							name = city_archery
							duration = -1
						}
					}
					50 = {
						add_province_modifier = { 
							name = city_pike
							duration = -1
						}
					}
				}
				if = {
					limit = { 
						OR = {
							culture = italian
							culture = dutch
						}
						has_province_modifier = city_archery
					}
					random = {
						chance = 50
						remove_province_modifier = city_archery
						add_province_modifier = { 
							name = city_pike
							duration = -1
						}
					}
				}
				if = {
					limit = { 
						OR = {
							culture = persian
							culture = catalan
							culture = alan
						}
						has_province_modifier = city_pike
					}
					random = {
						chance = 50
						remove_province_modifier = city_pike
						add_province_modifier = { 
							name = city_archery
							duration = -1
						}
					}
				}
				if = {
					limit = { 
						OR = {
							province_id = 230 #Specific province.  Must come after cultures.
							province_id = 231
							province_id = 232
							province_id = 233
							province_id = 234
						}
						has_province_modifier = city_pike
					}
					random = {
						chance = 75
						remove_province_modifier = city_pike
						add_province_modifier = { 
							name = city_archery
							duration = -1
						}
					}
				}
				
				#Adds Buildings
				add_province_modifier = {
					name = building_setup
					duration = 120
					#hidden = yes
				}				
			}
		}
		revoke_allowed = {
			always = no
		}
		ai_will_do = {
			factor = 1
		}
	}
	
}
